package com.taomu.app.zongchouhuli.weight.popwindow;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;

public class PopSelectPayViewManager implements CommonPopupWindow.ViewInterface{

    private Context mContext;

    private TextView mAlipayTv;

    private TextView mWechatTv;

    private TextView mBankTv;

    private CommonPopupWindow mCommonPop;

    public PopSelectPayViewManager(Context mContext){
        this.mContext = mContext;
    }

    @Override
    public void getChildView(View view, int layoutResId) {
        initView(view);
        initEvent();
    }

    private void initView(View view){
        mAlipayTv = view.findViewById(R.id.pop_selectpayway_alipay_tv);
        mWechatTv = view.findViewById(R.id.pop_selectpayway_wechat_tv);
        mBankTv = view.findViewById(R.id.pop_selectpayway_bank_tv);
    }

    private void initEvent(){
        mAlipayTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onItemClick(mAlipayTv.getText().toString());
                }
                onDismiss();
            }
        });
        mWechatTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onItemClick(mWechatTv.getText().toString());
                }
                onDismiss();
            }
        });
        mBankTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onItemClick(mBankTv.getText().toString());
                }
                onDismiss();
            }
        });
    }

    public void showPopWindow(View view){
        if (mCommonPop != null && mCommonPop.isShowing()) {
            return;
        } else {
            if (mCommonPop != null) {

            } else {
                mCommonPop = new CommonPopupWindow.Builder(mContext)
                        .setView(R.layout.pop_selectpayway)
                        .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                        .setAnimationStyle(R.style.AnimDown)
                        .setViewOnclickListener(this)
                        .setOutsideTouchable(true)
                        .create();
            }
            mCommonPop.showAsDropDown(view);
        }
    }

    public void onDismiss(){
        if(mCommonPop != null){
            mCommonPop.dismiss();
        }
    }

    private PopSelectPayWayListener listener;

    public void setPopSelectPayWayListener(PopSelectPayWayListener listener){
        this.listener = listener;
    }

    public interface PopSelectPayWayListener{
        void onItemClick(String type);
    }
}
